package kni.hoid;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

public class SearchActivity extends Activity {
	private static final int RECOGNIZER_RESULT = 1234;
	private static final int RECOGNIZER_RESULT2 = 1235;
	private static final String ID_FIELD = "id";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);

		ImageButton startSpeech1 = (ImageButton) findViewById(R.id.getSpeechButton1);
		startSpeech1.setImageResource(R.drawable.speaker);
		startSpeech1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "cs_CZ");
				intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to text");
				intent.putExtra(ID_FIELD, "first");
				startActivityForResult(intent, RECOGNIZER_RESULT);
			}
		});

		ImageButton startSpeech2 = (ImageButton) findViewById(R.id.getSpeechButton2);
		startSpeech2.setImageResource(R.drawable.speaker);
		startSpeech2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

				intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "cs_CZ");
				intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to text");
				intent.putExtra(ID_FIELD, "second");
				startActivityForResult(intent, RECOGNIZER_RESULT2);
			}
		});

		final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
				this, R.array.search_fields,
				android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter1);

		final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
				this, R.array.search_fields,
				android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner2.setAdapter(adapter2);

		Button find = (Button) findViewById(R.id.find);
		find.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				EditText searchText1 = (EditText) findViewById(R.id.firstEdit);
				EditText searchText2 = (EditText) findViewById(R.id.secondEdit);

				Item first = new Item();
				first.setType(resolveType(spinner1.getSelectedItem().toString()));
				first.setWhat(searchText1.getText().toString());

				Item second = new Item();
				second.setType(resolveType(spinner2.getSelectedItem()
						.toString()));
				second.setWhat(searchText2.getText().toString());

				List<Item> searchList = Arrays.asList(first, second);
				SearchService vufind = new VufindSearchService();
				SearchService google = new GoogleBooksService();
				final List<Book> vufindResults = vufind.search(searchList);
				//final List<Book> vufindResults = Collections.<Book>emptyList();
				final List<Book> gbooksResults = google.search(searchList);
				
				TextView vufindTextView = (TextView) findViewById(R.id.vufindResults);
				vufindTextView.setText("Vufind MZK("
						+ (vufindResults == null ? "0" : vufindResults.size())
						+ ")");
				vufindTextView.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent().setClass(
								SearchActivity.this, VufindActivity.class);
						intent.putExtra("result", (Serializable) vufindResults);
						startActivity(intent);
					}
				});

				TextView gbooksTextView = (TextView) findViewById(R.id.gbooksResults);
				gbooksTextView.setText("Google Books ("
						+ (gbooksResults == null ? "0" : gbooksResults
								.size()) + ")");
				gbooksTextView.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent().setClass(
								SearchActivity.this, VufindActivity.class);
						//List<String> reduced = gbooksResults.subList(0, 10);
						intent.putExtra("result", (Serializable) gbooksResults);
						startActivity(intent);
					}
				});
			}
		});

	}

	private Item.Type resolveType(String text) {
		if ("Titul".equals(text)) {
			return Item.Type.TITLE;
		} else if ("Autor".equals(text)) {
			return Item.Type.AUTHOR;
		} else if ("ISBN".equals(text)) {
			return Item.Type.ISBN;
		} else if ("Vše".equals(text)) {
			return Item.Type.ALL_FIELDS;
		}
		return null;
	}

	/**
	 * Handle the results from the recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ((requestCode == RECOGNIZER_RESULT || requestCode == RECOGNIZER_RESULT2)
				&& resultCode == RESULT_OK) {
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			TextView speechText = (TextView) findViewById(requestCode == RECOGNIZER_RESULT ? R.id.firstEdit
					: R.id.secondEdit);
			speechText.setText(matches.get(0).toString());
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
